package cn.cdeden.knowledge.controller.team;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.dto.TeamTemplateTreeDto;
import cn.cdeden.knowledge.domain.dto.TeamTemplateTreeQueryParam;
import cn.cdeden.knowledge.enums.ResultEnum;
import cn.cdeden.knowledge.enums.TeamParamEnum;
import cn.cdeden.knowledge.service.TeamFilesTreeService;
import cn.cdeden.knowledge.service.TeamMemberService;
import cn.cdeden.knowledge.service.TeamRelevanceRoleService;
import cn.cdeden.knowledge.service.TeamTemplateTreeService;
import cn.cdeden.system.api.model.LoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * @author Chen
 * @date 2024-05-17
 **/
@RestController
@RequiredArgsConstructor
//@Api(tags = "团队模板管理")
@RequestMapping("/api/teamTemplateTree")
public class TeamTemplateTreeController {

    private final TeamTemplateTreeService teamTemplateTreeService;

    private final TeamMemberService teamMemberService;

    private final TeamFilesTreeService teamFilesTreeService;

    private final TeamRelevanceRoleService teamRelevanceRoleService;

    @GetMapping
//    @Log("查询团队模板")
//    @ApiOperation("查询团队模板")
    public R<TableDataInfo<TeamTemplateTreeDto>> query(TeamTemplateTreeQueryParam query, Pageable pageable) {
        return R.ok(teamTemplateTreeService.queryAll(query, pageable));
    }


    @PostMapping("/uploadTemplate")
//    @Log("上传模板")
//    @ApiOperation("上传模板")
    public R updateCommunity(@ModelAttribute TeamTemplateTreeDto teamTemplateTreeDto) throws Exception {
        //验证是否拥有模板权限
        Long teamId = teamTemplateTreeDto.getTeamId();
        if (!teamMemberService.checkPermission(String.valueOf(teamId), LoginHelper.getLoginUser(), TeamParamEnum.TEMPLATE.getParam())) {
            return R.fail(ResultEnum.PERM_ERROR.getNum(), "无模板权限，请联系管理员");
        }
        teamTemplateTreeService.uploadTemplate(teamTemplateTreeDto.getImage(), teamTemplateTreeDto.getFile(), teamTemplateTreeDto.getRealName(), teamId);
        return R.ok();
    }

    @PostMapping("/updateTemplate")
//    @ApiModelProperty("更新模板信息")
    public R updateTemplate(@ModelAttribute TeamTemplateTreeDto teamTemplateTreeDto) {
        //验证是否拥有模板权限
        if (!teamMemberService.checkPermission(String.valueOf(teamTemplateTreeDto.getTeamId()), LoginHelper.getLoginUser(), TeamParamEnum.TEMPLATE.getParam())) {
            return R.fail(ResultEnum.PERM_ERROR.getNum(), "无模板权限，请联系管理员");
        }
        teamTemplateTreeService.updateTemplate(teamTemplateTreeDto.getId(), teamTemplateTreeDto.getRealName());
        return R.ok();
    }

    @GetMapping("/createFileFormTemplate")
//    @ApiOperation("从模板库使用文档")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "templateId", value = "模板id", dataType = "Long", required = true),
//            @ApiImplicitParam(name = "parentFolderId", value = "树id", dataType = "Long", required = true),
//            @ApiImplicitParam(name = "fileName", value = "文件名称", dataType = "String", required = true),
//            @ApiImplicitParam(name = "type", value = "模板文件类型", dataType = "String", required = false),
//            @ApiImplicitParam(name = "teamId", value = "团队Id", dataType = "String", required = true)
//    })
    public R queryCollectionTemplate(@RequestParam(required = false) Long templateId,
                                                  @RequestParam String teamId,
                                                  @RequestParam String fileName,
                                                  @RequestParam String parentFolderId,
                                                  @RequestParam String type) {
        //团队角色权限
        if (!teamRelevanceRoleService.checkPermission(parentFolderId, teamId, LoginHelper.getLoginUser(), TeamParamEnum.ADD.getParam())) {
            return R.fail(ResultEnum.PERM_ERROR.getNum(), "无新建权限，请联系管理员");
        }
        return R.ok(teamFilesTreeService.createFileFromTemplate(templateId, Long.valueOf(parentFolderId), fileName, type, "team"));
    }


    /**
     * 删除模板通过模板id
     *
     * @param id 模板id
     * @return
     */
//    @ApiOperation("删除模板通过模板id")
    @GetMapping("/deleteTemplateById")
    public R deleteTemplateById(Long id, String teamId) {
        //验证是否拥有模板权限
        if (!teamMemberService.checkPermission(teamId, LoginHelper.getLoginUser(), TeamParamEnum.TEMPLATE.getParam())) {
            return R.fail(ResultEnum.PERM_ERROR.getNum(), "无模板权限，请联系管理员");
        }
        teamTemplateTreeService.removeTeamTemplate(id);

        return R.ok();
    }

//    @ApiOperation("删除全部模板")
    @GetMapping("/deleteAllByTeamId")
    public R deleteAllByTeamId(String teamId) {
        //验证是否拥有模板权限
        if (!teamMemberService.checkPermission(teamId, LoginHelper.getLoginUser(), TeamParamEnum.TEMPLATE.getParam())) {
            return R.fail(ResultEnum.PERM_ERROR.getNum(), "无模板权限，请联系管理员");
        }
        teamTemplateTreeService.deleteAllByTeamId(teamId);

        return R.ok();
    }

    @GetMapping("/getTemplateById")
//    @ApiOperation("查询指定模板")
    public R getTemplateById(Long id) {
        return R.ok(teamTemplateTreeService.getById(id));
    }

    /**
     * 批量上传
     *
     * @param teamTemplateTreeDto
     * @return
     */
    @PostMapping("/batchUpload")
    public R batchUpload(@ModelAttribute TeamTemplateTreeDto teamTemplateTreeDto) {
        teamTemplateTreeService.uploadTemplateAdmin(teamTemplateTreeDto.getTeamId(), teamTemplateTreeDto.getFiles());
        return R.ok();
    }
}
